package org.herrdommel.webpagechecker.pages;

import java.util.Calendar;

import org.herrdommel.webpagechecker.R;
import org.herrdommel.webpagechecker.util.DownloadPageTask;
import org.htmlcleaner.TagNode;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.text.Html;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class DortmundFragment extends AbstractPage {

	public static String DORTMUND = "Dortmund";
	private static String DORTMUND_DOMAIN = "http://www.dortmund.de";
	private static String DORTMUND_MAINPAGE = "/de/leben_in_dortmund/sicherheit_und_recht/ordnungsamt/verkehrsueberwachung_2/geschwindigkeitskontrollen/index.html";

	private TextView tvResult = null;
	private ProgressDialog pdLoadData = null;

	@Override
	public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
		setHasOptionsMenu(true);
		tvResult = new TextView(getActivity());
		tvResult.setGravity(Gravity.CENTER);
		final String cacheResult = checkCache(DORTMUND, Calendar.WEEK_OF_YEAR);
		if (cacheResult == null) {
			findDetailPage();
		} else {
			tvResult.setText(Html.fromHtml(cacheResult));
		}
		return tvResult;
	}

	@Override
	public void onRadarLoaded(final TagNode content) {
		switch (currentStep) {
			case STEP_ONE :
				currentStep = STEP_TWO;
				findResults(content);
				break;
			case STEP_TWO :
				currentStep = STEP_FINISH;
				displayResults(content);
				break;

			default :
				if (pdLoadData.isShowing()) {
					pdLoadData.dismiss();
				}
				currentStep = STEP_FINISH;
				tvResult.setText(R.string.content_about);
				break;
		}
	}

	private void findDetailPage() {
		currentStep = STEP_ONE;
		pdLoadData = ProgressDialog.show(getActivity(), getString(R.string.progress_message_loading),
				String.format(getString(R.string.progress_message_loading_data), DORTMUND), true, true,
				new OnCancelListener() {

					@Override
					public void onCancel(final DialogInterface dialog) {
						currentStep = STEP_FINISH;
						tvResult.setText(R.string.content_loading_canceled);
						pdLoadData.cancel();
						pdLoadData.dismiss();
					}
				});
		new DownloadPageTask(this).execute(DORTMUND_DOMAIN + DORTMUND_MAINPAGE);
	}

	private void findResults(final TagNode content) {
		String detailPage = null;
		final TagNode node = content.findElementByAttValue("class", "element", true, true);
		for (final TagNode aNode : node.getAllElements(true)) {
			if (aNode.getName().equals("a")) {
				detailPage = aNode.getAttributeByName("href");
				break;
			}
		}
		new DownloadPageTask(this).execute(DORTMUND_DOMAIN + detailPage);
	}

	private void displayResults(final TagNode content) {
		pdLoadData.setMessage(String.format(getString(R.string.progress_message_process_data), DORTMUND));
		final StringBuffer sb = new StringBuffer();
		final TagNode node = content.findElementByAttValue("class", "hauptText", true, true);
		for (final TagNode aNode : node.getAllElements(true)) {
			if (aNode.getName().equals("h4")) {
				sb.append("<b><u>" + aNode.getText() + "</u></b><br />");
			} else if (aNode.getName().equals("li")) {
				sb.append(aNode.getText() + "<br />");
			}
		}
		final String result = sb.toString();
		tvResult.setText(Html.fromHtml(result));
		pdLoadData.dismiss();
		saveToCache(DORTMUND, result);
	}

	@Override
	public boolean onOptionsItemSelected(final MenuItem item) {
		switch (item.getItemId()) {
			case R.id.menu_reload :
				currentStep = STEP_INIT;
				findDetailPage();
				return true;

			default :
				break;
		}
		return super.onOptionsItemSelected(item);
	}

}
